package com.linghu.service;

import com.linghu.model.House;

public class HouseService {
    private House house[];//保存house对象

    private int houseNums=1;//记录有多少个房屋信息
    private int idCounter=1;//记录id自增长信息


    public HouseService(int size) {
        house=new House[size];
        house[0]=new House(1,"jack","123","海淀区",2000,"未出租");
    }



    public House findById(int findId){
        for (int i=0;i<houseNums;i++){
            if (house[i].getId()==findId){
               return house[i];
            }
        }
        return null;
    }

    public boolean del(int delId){
        int index=-1;
        for (int i=0;i<houseNums;i++){
            if (delId==house[i].getId()){
                index=i;
            }
        }
        if (index==-1){
            return false;
        }

        for (int i=index;i<houseNums-1;i++){
            house[i]=house[i+1];
        }
        house[houseNums-1]=null;
        houseNums--;
        return true;

    }

    public boolean add(House newhouse){
        if (houseNums==house.length){
            return false;
        }
        house[houseNums]=newhouse;
        houseNums++;
        //
        idCounter++;
        newhouse.setId(idCounter);
        return true;
    }

    public House[] list(){
        return house;
    }
}
